﻿#If NETFX_CORE Then
Imports Windows.Storage
Imports System.Runtime
Imports System.IO
Imports System.Runtime.InteropServices.WindowsRuntime

Namespace Global.System.IO.Compression

    Public Module ZipFileExtensions

        ''' <summary>
        ''' Extract data to the given full directory path.
        ''' </summary>
        ''' <param name="source"></param>
        ''' <param name="destinationDirectoryName"></param>
        ''' <remarks></remarks>
        <Extension()> _
        Public Sub ExtractToDirectory(ByVal source As ZipArchive, ByVal destinationDirectoryName As String)
            If source Is Nothing Then
                Throw New ArgumentNullException("source")
            End If
            If destinationDirectoryName Is Nothing Then
                Throw New ArgumentNullException("destinationDirectoryName")
            End If
            Dim dir = StorageFolder.GetFolderFromPathAsync(destinationDirectoryName).GetResults()            
            Dim fullName As String = Directory.CreateDirectory(destinationDirectoryName)
            Dim entry As ZipArchiveEntry
            For Each entry In source.Entries
                Dim fullPath As String = Path.Combine(fullName, entry.FullName)
                If Not fullPath.StartsWith(fullName, StringComparison.OrdinalIgnoreCase) Then
                    Throw New IOException("IO_ExtractingResultsInOutside")
                End If
                If Path.GetFileName(fullPath).Length = 0 Then
                    If entry.Length <> 0 Then
                        Throw New IOException("IO_DirectoryNameWithData")
                    End If
                    Directory.CreateDirectory(fullPath)
                    Continue For
                End If
                Directory.CreateDirectory(Path.GetDirectoryName(fullPath))
                ZipFileExtensions.ExtractToFile(entry, fullPath, False)
            Next
        End Sub

        <Extension()> _
        Public Sub ExtractToFile(ByVal source As ZipArchiveEntry, ByVal destinationFileName As String)
            ZipFileExtensions.ExtractToFile(source, destinationFileName, False)
        End Sub

        <Extension> _
        Public Sub ExtractToFile(ByVal source As ZipArchiveEntry, ByVal destinationFileName As String, ByVal overwrite As Boolean)
            If source Is Nothing Then
                Throw New ArgumentNullException("source")
            End If
            If destinationFileName Is Nothing Then
                Throw New ArgumentNullException("destinationFileName")
            End If
            Dim mode As FileMode = If(overwrite, FileMode.Create, FileMode.CreateNew)
            Dim dstDir = StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(destinationFileName)).GetResults()
            Dim dstFn = Path.GetFileName(destinationFileName)
            If overwrite = True Then
                Using stream As Stream = dstDir.OpenStreamForWriteAsync(dstFn, CreationCollisionOption.ReplaceExisting).Result
                    Using stream2 As Stream = source.Open
                        stream2.CopyTo(stream)
                    End Using
                End Using
            Else
                Using stream As Stream = dstDir.OpenStreamForWriteAsync(dstFn, CreationCollisionOption.FailIfExists).Result
                    Using stream2 As Stream = source.Open
                        stream2.CopyTo(stream)
                    End Using
                End Using
            End If
        End Sub

    End Module

End Namespace

#End If